package com.sinosoft.vaccinetoai.utils.validator;

import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiazhihao
 * @ClassName: FileValidatorUtil
 * @ClassNameExplain:
 * @Description: 文件校验工具类
 * @date 2023/7/6 11:38
 */
public class FileValidatorUtil {

    /**
     * 允许的图片类型格式
     */
    private static final List<String> allowedImageExtensions = Arrays.asList("jpeg", "png","jpg","gif","tiff","webp","bmp");

    /**
     * @Title: isImgType
     * @TitleExplain:
     * @Description:  是否是图片格式
     * @param file 前端传入文件
     * @return boolean
     * @version 1.0.0
     * @author xiazhihao
     */
    public static boolean isImgType(MultipartFile file){
        // 获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        if (originalFilename != null && !originalFilename.isEmpty()) {
            //获取扩展名
            String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            return allowedImageExtensions.stream()
                    .anyMatch(ext -> ext.equalsIgnoreCase(extension));
        }
        return false;
    }

}
